home *** CD-ROM | disk | FTP | other *** search
/ Complete Linux / Complete Linux.iso / docs / system / mail / transpor / ifmail23.z / ifmail23 / ifmail / iflib / ulock.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-05-03  |  2.2 KB  |  116 lines

  1. #include <sys/types.h>
  2. #include <unistd.h>
  3. #include <stdio.h>
  4. #include <string.h>
  5. #include <errno.h>
  6. #include <ctype.h>
  7. #include <signal.h>
  8. #include <sys/stat.h>
  9. #include "lutil.h"
  10.  
  11. #ifndef LOCKDIR
  12. #define LOCKDIR "/usr/spool/uucp"
  13. #endif
  14.  
  15. #define LCKPREFIX LOCKDIR"/LCK.."
  16. #define LCKTMP LOCKDIR"/TMP."
  17.  
  18. #ifdef DONT_HAVE_PID_T
  19. #define pid_t int
  20. #endif
  21.  
  22. int lock(line)
  23. char *line;
  24. {
  25.     pid_t    mypid,rempid;
  26.     char    tmpname[256],lckname[256];
  27.     char    *p;
  28.     int    i,rc;
  29.     FILE    *f;
  30.  
  31.     rc=-1;
  32.     if ((p=strrchr(line,'/')) == NULL) p=line; else p++;
  33.     mypid=getpid();
  34.     sprintf(tmpname,"%s%d",LCKTMP,mypid);
  35.     if ((f=fopen(tmpname,"w")) == NULL)
  36.     {
  37.         debug(4,"lock cannot create %s");
  38.         return(-1);
  39.     }
  40. #if defined(ASCII_LOCKFILES)
  41.     fprintf(f,"%10d\n",mypid);
  42. #elif defined(BINARY_LOCKFILES)
  43.     fwrite(&mypid,sizeof(mypid),1,f);
  44. #else
  45. #error Must define ASCII_LOCKFILES or BINARY_LOCKFILES
  46. #endif
  47.     fclose(f);
  48.     chmod(tmpname,0444);
  49.     sprintf(lckname,"%s%s",LCKPREFIX,p);
  50.     p=lckname+strlen(lckname)-1;
  51.     *p=tolower(*p);
  52.     debug(4,"Trying to create %s for %d",lckname,mypid);
  53.     for (i=0; (i++<5) && ((rc=link(tmpname,lckname)) != 0) && 
  54.                 (errno == EEXIST); )
  55.     {
  56.         if ((f=fopen(lckname,"r")) == NULL)
  57.         {
  58.             debug(4,"cannot open existing lock file");
  59.         }
  60.         else
  61.         {
  62. #if defined(ASCII_LOCKFILES)
  63.             fscanf(f,"%d",&rempid);
  64. #elif defined(BINARY_LOCKFILES)
  65.             fread(&rempid,sizeof(rempid),1,f);
  66. #endif
  67.             fclose(f);
  68.             debug(4,"lock file read for process %d",rempid);
  69.         }
  70.         if (kill(rempid,0) && (errno ==  ESRCH))
  71.         {
  72.             debug(4,"process inactive, unlink file");
  73.             unlink(lckname);
  74.         }
  75.         else
  76.         {
  77.             debug(4,"process active, sleep a bit");
  78.             sleep(2);
  79.         }
  80.     }
  81.     unlink(tmpname);
  82.     debug(4,"lock result %d (errno %d)",rc,errno);
  83.     return(rc);
  84. }
  85.  
  86. int ulock(line)
  87. char *line;
  88. {
  89.     int    mypid,rempid;
  90.     char    lckname[256];
  91.     char    *p;
  92.     int    rc;
  93.     FILE    *f;
  94.  
  95.     rc=-1;
  96.     if ((p=strrchr(line,'/')) == NULL) p=line; else p++;
  97.     mypid=getpid();
  98.     sprintf(lckname,"%s%s",LCKPREFIX,p);
  99.     p=lckname+strlen(lckname)-1;
  100.     *p=tolower(*p);
  101.     if ((f=fopen(lckname,"r")) == NULL)
  102.     {
  103.         logerr("$cannot open lock file %s",lckname);
  104.         return rc;
  105.     }
  106. #if defined(ASCII_LOCKFILES)
  107.     fscanf(f,"%d",&rempid);
  108. #elif defined(BINARY_LOCKFILES)
  109.     fread(&rempid,sizeof(rempid),1,f);
  110. #endif
  111.     fclose(f);
  112.     if (rempid ==  mypid)
  113.         rc=unlink(lckname);
  114.     return(rc);
  115. }
  116.